<?php
	require('gym.doc.php');
	//健身房列表
	$app->get(
		'/gym/list',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			foreach($db2->select(0, 'tb_gym', '*') as $v){
				$tmp['gymid'] = $v['tbid'];
				$tmp['name'] = $v['name'];
				$dom = explode($file_subsplit, $v['img']);
				$tmp['img'] = $dom[0];
				$tmp['simg'] = getFileInfo($dom[0], 'simg');
				$tmp['address'] = $v['address'];
				$tmp['position'] = explode(',', $v['position']);
				$tmp['summary'] = $v['summary'];
				$echo['data'][] = $tmp;
				unset($tmp);
			}
			echo json_encode($echo);
		}
	);
	//健身房详情
	$app->get(
		'/gym/detail',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['gymid'])){
				$gym=$db2->select(1, 'tb_gym', '*',array('tbid'=>$data['gymid']));
				$tmp['gymid'] = $gym['tbid'];
				$tmp['name'] = $gym['name'];
				$dom = explode($file_subsplit, $gym['img']);
				$tmp['img'] = $dom[0];
				$tmp['simg'] = getFileInfo($dom[0], 'simg');
				$tmp['address'] = $gym['address'];
				$tmp['position'] = explode(',', $gym['position']);
				$tmp['summary'] = $gym['summary'];
				$echo['data'][] = $tmp;
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//健身房在线用户
	$app->get(
		'/gym/onlineuser',
		function() use($app, $db2){
		    
		    $tmp_arr = array();
		    
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['gymid']) && isset($data['userid']) && isset($data['usertype'])){
				$echo['data'] = array();
				$sql1 = $db2->select(4, 'tb_member', 'nickname', array('#tb_member.tbid' => 'tb_sign.userid'));
				$sql2 = $db2->select(4, 'tb_coach', 'nickname', array('#tb_coach.tbid' => 'tb_sign.userid'));
				$case = 'CASE usertype when 1 then ('.$sql1.') else ('.$sql2.') end';
				$query = 'SELECT * from (select *,'.$case.' as nickname from tb_sign where gym_id = '.$data['gymid'].' and DATE_FORMAT(dt, "%Y-%m-%d") = CURDATE() AND TIMESTAMPDIFF(HOUR, dt, NOW()) < CASE usertype WHEN 1 THEN 3 ELSE 12 END';
				if(isset($data['findusertype'])){
					$query .= ' and usertype = '.$data['findusertype'];
				}
				if(isset($data['sextype'])){
					$sql1 = $db2->select(4, 'tb_member', 'sex', array('#tb_member.tbid' => 'tb_sign.userid'));
					$sql2 = $db2->select(4, 'tb_coach', 'sex', array('#tb_coach.tbid' => 'tb_sign.userid'));
					$case2 = 'CASE usertype when 1 then ('.$sql1.') else ('.$sql2.') end';
					$query .= ' and '.$case2.' like "%'.$data['sextype'].'%"';
				}
				if(isset($data['nickname'])){
					$query .= ' and '.$case.' like "%'.$data['nickname'].'%"';
				}
				$query.=') as tb_sign';
				$query .= ' group by userid, usertype';
				$query .= ' having mod(count(*), 2) = 1 order by case when userid='.$data['userid'].' and usertype='.$data['usertype'].' then 0 else 1 end,dt asc';
				$query .= ' limit '.$data['from'].', '.$data['to'];
				$db2->query($query);
				$echo['member'] = 0;
				$echo['coach'] = 0;
				$rs = $db2->query($query)->fetchAll();
				$echo['count'] = count($rs);
				foreach($rs as $v){
					$tb = $v['usertype'] == 1 ? 'tb_member' : 'tb_coach';
					$user = $db2->select(1, $tb, '*', array('tbid' => $v['userid']));
					$tmp['userid'] = $v['userid'];
					$tmp['usertype'] = $v['usertype'];
					$tmp['mobile'] = substr_replace($user['mobile'], '****', 3, 4);
					if($v['usertype'] == 1){
						$echo['member'] += 1;
						$tmp['head'] = file_exists('dofiles/member/'.$v['userid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$v['userid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						$tmp['isvip'] = date('Y-m-d H:i:s', strtotime($v['privilege_dt'])) > date('Y-m-d H:i:s') ? true : false;
					}else{
						$echo['coach'] += 1;
						$tmp['head'] = file_exists('dofiles/coach/'.$v['userid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$v['userid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						$tmp['isvip'] = false;
					}
					$follow = $db2->select(1, 'tb_follow', '*', array(
						'follow_userid' => $v['userid'],
						'follow_usertype' => $v['usertype'],
						'userid' => $data['userid'],
						'usertype' => $data['usertype']
					));
					$tmp['isfollow'] = $follow ? true : false;
					$tmp['nickname'] = $user['nickname'];
					$tmp['sex'] = $user['sex'];
					$tmp['dt'] = $v['dt'];
					
					if($tmp['userid'] != $data['userid'] && $tmp['isfollow'] == false){
					    array_push($tmp_arr, $tmp);
					    continue;
					}
					
					$echo['data'][] = $tmp;
					unset($tmp);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			
			foreach($tmp_arr as $value){
			    array_push($echo['data'], $value);
			}
			
			echo json_encode($echo);
		}
	);
	//登录
	$app->post(
		'/gym/login',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['name'])){
				$gym = $db2->select(1, 'tb_gym', '*', array('name' => $data['name']));
				if($gym){
					if(isset($data['password']) && $gym['password'] == $data['password']){
						//健身房id、健身房名称、健身房图片地址
						$echo['gymid'] = $gym['tbid'];
						$echo['name'] = $gym['name'];
						$echo['img'] = $gym['img'];
					}else{
						$echo['error'] = "登录密码错误";
					}
				}else{
					$echo['error'] = "健身房不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//健身房交易记录
	$app->get(
		'/gym/trade',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['gymid'])){
				//真实姓名、会员ID(手机号)、消费时间、应付金额、实付金额、消费ADAA币、类型（会员/教练）、VIP会员到期时间、教练等级
				$data=$db2->select(0,'tb_order','*',array("#date_format(dt,'%Y-%m-%d')"=>'curdate()','gym_id'=>$data['gymid'],'state'=>3,'order'=>'dt desc'));
				foreach($data as $value){
					if($value['usertype']==1){
						$tb='tb_member';
					}else{
						$tb='tb_coach';
					}
					$user=$db2->select(1,$tb,'*',array('tbid'=>$value['userid']));
					$temp['realname']=$user['realname'];
					$temp['mobile']=$user['mobile'];
					$temp['dt']=$value['dt'];
					$temp['price']='¥'.$value['price'];
					$temp['trademoney']=$db2->select(3,'tb_trade','trademoney',array('orderid'=>$value['idcode'],'type'=>0));
					if(!$temp['trademoney']){
						$temp['trademoney']=0;
					}else{
						$temp['trademoney']='¥'.abs($temp['trademoney']);
					}
					$temp['tradeadaa']=$db2->select(3,'tb_adaa','tradeadaa',array('orderid'=>$value['idcode'],'type'=>2));
					if(!$temp['tradeadaa']){
						$temp['tradeadaa']=0;
					}else{
						$temp['tradeadaa']='¥'.abs($temp['tradeadaa']);
					}
					if($value['usertype']==1){
						$tb='tb_member';
						$temp['type']='会员';
						if(strtotime($user['privilege_dt'])>=time()){
							$temp['privilege_dt']=$user['privilege_dt'];
						}else{
							$temp['privilege_dt']='非VIP会员';
						}
					}else{
						$tb='tb_coach';
						$temp['type']='教练';
						switch($user['level']){
							case 0:$temp['level']='淘汰';break;
							case 1:$temp['level']='银牌';break;
							case 2:$temp['level']='金牌';break;
							case 3:$temp['level']='钻石';break;
						}
					}
					$echo['data'][]=$temp;
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//健身房场地课程
	$app->get(
		'/gym/place/curriculum',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['gymid']) && isset($data['type']) && isset($data['from']) && isset($data['to'])){
				$where=array('order'=>'tb_place_curriculum.tbid desc',"#STR_TO_DATE(CONCAT(tb_place_snap.reservedt,' ',tb_place_snap.reservetime),'%Y-%m-%d %H')[>]"=>"DATE_ADD(now(),INTERVAL 1 HOUR)",'tb_place_snap.gym_id'=>$data['gymid']);
				if($data['type']==1){
					$where['isfree']=1;
				}else if($data['type']==2){
					$where['isfree']=0;
				}
				$echo['count']=$db2->select(2,'tb_place_curriculum',array('[><]tb_place_snap'=>array('place_id'=>'tbid')),'*',$where);
				$where['limit'] = array($data['from'], $data['to']);
				//$db2->set('debug',1);
				$curriculum=$db2->select(0,'tb_place_curriculum',array('[><]tb_place_snap'=>array('place_id'=>'tbid')),'tb_place_curriculum.tbid,tb_place_curriculum.title,tb_place_curriculum.img,tb_place_curriculum.intro,tb_place_curriculum.introimg,tb_place_curriculum.people,tb_place_curriculum.price,tb_place_curriculum.isfree,tb_place_snap.reservedt,tb_place_snap.reservetime,('.$db2->select(4,'tb_place_curriculum_snap','count(*)',array('#tb_place_curriculum_snap.place_curriculum_id'=>'tb_place_curriculum.tbid')).')[count]',$where);
				foreach($curriculum as $value){
					$tmp['tbid'] =$value['tbid'];
					$tmp['title'] =$value['title'];
					$tmp['img'] = SITEROOTURL.$value['img'];
					$tmp['intro'] = $value['intro'];
					$tmp['introimg'] = $value['introimg'];
					$tmp['people'] = $value['people'];
					$tmp['price'] = $value['price'];
					$tmp['count'] = $value['count'];
					$tmp['reservedt'] = $value['reservedt'];
					$tmp['reservetime'] = $value['reservetime'];
					$tmp['isfree'] = $value['isfree'];
					$echo['data'][]=$tmp;
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
